package com.tgy.service.user.userServiceImpl;

import com.tgy.dao.user.UserDao;
import com.tgy.dao.user.userDaoImpl.UserDaoImpl;
import com.tgy.entity.SmbmsUser;
import com.tgy.service.user.UserService;
import org.junit.Test;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

/***
 * @ClassName: UserServiceImpl
 * @Description:
 * @Auther: 送一一块二向箔
 * @Date: 22:58 2021/12/9
 * @version : V1.0
 */
public class UserServiceImpl implements UserService {
    //业务层调用Dao层做数据库交互
    private UserDao userDao;

    public UserServiceImpl() {
        userDao = new UserDaoImpl();
    }

    @Override
    public SmbmsUser login(String username, String password) {
        SmbmsUser smbmsUser = new SmbmsUser();
        try {
            smbmsUser = userDao.getLoginUser(username);
            if (Objects.equals(smbmsUser.getUserPassword(),password)) {
                System.out.println(smbmsUser.getUserPassword());
                System.out.println("密码正确");
            }else {
                System.out.println(smbmsUser.getUserPassword());
                System.out.println("密码错误");
            }

        }catch (Exception e){
            e.printStackTrace();
        }

        return smbmsUser;
    }

    @Override
    public boolean updPassword(String username, String password) {
        boolean flag =false;
        try {
            if(userDao.changePassword(username,password) >0){
              flag = true;
            }
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }finally {
            return flag;
        }
    }

    @Override
    public int getUserByLimit(String name, int role) {
        return  this.userDao.getNum(name,role);
    }

    @Override
    public List<SmbmsUser> getUserList(String name, int roleCode, int currentPageNo, int pageSize) {
        List<SmbmsUser> smbmsUsers = new ArrayList<>();
        try {
            smbmsUsers = this.userDao.getUserList(name,roleCode,currentPageNo,pageSize);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return smbmsUsers;
    }

    @Override
    public boolean add(SmbmsUser user) {
        return this.userDao.userAdd(user);
    }

}
